package persistence;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import model.Atributo;
import model.Entidad;
import model.Proyecto;

import com.thoughtworks.xstream.XStream;

/**
 * Clase singleton para la serializacion, deserealizacion y busqueda de todos los elementes del proyecto 
 */
public class XMLGeneral implements XMLPersistencia {
	private static XMLGeneral instance = null;
	
	/*
	 * Directorio del usuario 
	 */
	private String directorio;
	
	public String getDirectorio(){
		return this.directorio;
	}
	
	public void setDirectorio(String directorio){
		this.directorio = directorio;
	}

	private XMLGeneral() {
	}

	public static XMLGeneral getInstance() {
		if (XMLGeneral.instance == null) {
			XMLGeneral.instance = new XMLGeneral();
		}
		return XMLGeneral.instance;
	}
	
	
	public void create(Object object) {
		try {
			XStream xstream = this.createXStream();
			//Guardo el objeto 
			FileOutputStream file = new FileOutputStream(this.filesDirectory() + this.persistingFileName(object));
			xstream.toXML(object, file);
			file.close();
		} catch (Exception e) {
			System.out.println("No se pudo crear el XMLGeneral");
		}
	}
	
	
	public void update(Object object) {
		//Guardo el objeto modificado
		this.create(object);
	}
	
	
	public void delete(Object object) {
		File file = new File(this.filesDirectory() + this.persistingFileName(object));
		this.executeDelete(((Proyecto) object).getIdentificator(), file);
	}
	

	public void delete(String identificator) {
		File file = new File(this.filesDirectory() + identificator + ".xder");
		this.executeDelete(identificator, file);
	}

	private void executeDelete(String identificator, File file) {
		if (file.delete()){
			System.out.println("El XMLGeneral " + identificator + " fue borrado exitosamente");
			System.out.println();
		}else{
			System.out.println("El XMLGeneral " + identificator + " no pudo ser borrado");
			System.out.println();
		}
	}
	
	
	public XStream createXStream() {
		XStream xstream = new XStream();
		xstream.alias("Proyecto", Proyecto.class);
		xstream.alias("Entidad", Entidad.class);
		xstream.alias("Atributo", Atributo.class);

		return xstream;
	}
	
	
	public String persistingFileName(Object object) {
		return (((Proyecto) (object)).getIdentificator()) + ".xder";
	}
	
	
	public String filesDirectory() {
		
		//return "src\\" + this.directorio + "\\Datos\\";
		return this.directorio +"\\";
	}

	
	public Object findByIdentificator(String absolutePath) {
		Proyecto proyect = null;
		try {
			XStream xstream = this.createXStream();
			//FileInputStream file = new FileInputStream(this.filesDirectory() + identificator + ".xml");
			FileInputStream file = new FileInputStream(absolutePath);
			proyect = (Proyecto) xstream.fromXML(file);
			file.close();
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		}
		return proyect;
	}

	
	public List<Object> findAll() {
		List<Object> listCircuit = new ArrayList<Object>();
		try {
			XStream xstream = this.createXStream();
			Proyecto proyecto = null;
			File directory = new File(this.filesDirectory());
			String[] files = directory.list();
			for (int i = 0; i < files.length; ++i) {
				if(!files[i].equals(".svn")){
					FileInputStream file = new FileInputStream(this.filesDirectory() + files[i]);
					proyecto = (Proyecto) xstream.fromXML(file);
					listCircuit.add(proyecto);
					file.close();
				}
			}

		} catch (FileNotFoundException e) {
			
			return null;
		} catch (IOException e) {
		}
		return listCircuit;
	}
}
